package cn.ekuma.epos.productsfinishing;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JToolBar;

import cn.ekuma.data.ui.swing.AbstractDAOJEditor;
import cn.ekuma.data.ui.swing.JChildDAOEditorPanel;
import cn.ekuma.epos.businesslogic.ProductUtil;
import cn.ekuma.epos.core.util.ProductSwingUtil;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.epos.panel.AbstractJModelFinishingPanel;
import cn.ekuma.epos.qkorder.link.JOperationProductCategoryPanel;
import cn.ekuma.epos.qkorder.link.JProductBomIncludeStorePanel;
import cn.ekuma.epos.qkorder.link.JProductCodeSimilarPanel;
import cn.ekuma.epos.qkorder.link.JProductIncludeBomStorePanel;
import cn.ekuma.epos.qkorder.link.JSimilarProductStorePanel;

import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.bean.SharedProduct;
import com.openbravo.bean.erp.OperationProductCategory;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.loader.ui.JQBFParametersPanel;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.bean.ProductExt;
import com.openbravo.pos.inventory.AuxiliarEditor;
import com.openbravo.pos.inventory.model.BomProductTableModel;
import com.openbravo.pos.inventory.model.ProductAuxiliarTableModel;
import com.openbravo.pos.inventory.model.ProductCodeTableModel;
import com.openbravo.pos.inventory.model.ProductCustomerCategoryPriceModel;
import com.openbravo.pos.inventory.model.ProductPriceTableModel;
import com.openbravo.pos.inventory.model.ProductScoreTableModel;
import com.openbravo.pos.inventory.model.ProductStockLevelTableModel;
import com.openbravo.pos.panels.JDropProductByCategoryPanel;
import com.openbravo.pos.panels.JDropProductByConcernsPanel;
import com.openbravo.pos.panels.JDropProductByCustomerPanel;
import com.openbravo.pos.panels.JDropProductByOperationCategoryPanel;
import com.openbravo.pos.panels.JDropProductByProductPanel;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.CustomerChangedListener;
import com.openbravo.pos.panels.event.ProductCategoryChangedListener;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.link.JBomProductForIncludePanel;
import com.openbravo.pos.panels.link.JProductCustomerViewPanel;
import com.openbravo.pos.panels.link.JProductLotNoStorePanel;
import com.openbravo.pos.panels.link.JProductStorePanel;
import com.openbravo.pos.panels.model.ProductTableModel;
import com.openbravo.pos.panels.navigator.JCustomerNavigator;
import com.openbravo.pos.panels.navigator.JOperationProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.JProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.event.OperationProductCategoryChangedListener;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class JProductsFinishingPanel extends AbstractJModelFinishingPanel<Product,ProductExt>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8842624968131384340L;

	
	private JProductCategoryNavigator productCategoryNavigator;
	private JOperationProductCategoryNavigator operationProductCategoryNavigator;
	private JChildDAOEditorPanel bomProductPanel;
	private JChildDAOEditorPanel productCustomerPriceEditor;
	private JChildDAOEditorPanel productCodeEditor;
	private JChildDAOEditorPanel productAuxiliarEditor;
	private JChildDAOEditorPanel productStockLevelEditor;
	private JChildDAOEditorPanel productPriceEditor;
	private JChildDAOEditorPanel productScoreEditor;
	
	
	/**
	 * Create the panel.
	 */
	public JProductsFinishingPanel() {
		super(new ProductTableModel());
	}
	
	private JButton btnNewButton_6;


	private JCustomerNavigator customerNavigator;

	protected  void configToolBar(JToolBar jToolBar1){
	    btnNewButton_6 = new JButton("");
	    btnNewButton_6.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			jAddShardProduct();
		}
	   });
	   btnNewButton_6.setPreferredSize(new Dimension(50, 20));
	   btnNewButton_6.setIcon(new ImageIcon(AbstractJModelFinishingPanel.class.getResource("/com/openbravo/images/inbox.png")));
	   jToolBar1.add(btnNewButton_6);
	}
	
	protected void jAddShardProduct() {
		int selectedRow = jXTable1.getSelectedRow();
		if (selectedRow >= 0){
			Product p=tableModel.getObj(jXTable1.convertRowIndexToModel(selectedRow));
			 try {
	            	mode.getDLERP().saveSharedProduct(new SharedProduct(p.getID()));
	            } catch (BasicException ex) {
	                new MessageInf(ex).show(this);
	      };
		}
	}

	
	
	protected void listByProfits() {
		double form,to;
		try {
			form=(Double) Formats.DOUBLE.parseValue(ProfitsFrom.getText(),new Double(0));
			to=(Double) Formats.DOUBLE.parseValue(profitsTo.getText(),new Double(100000));
			tableModel.resert(mode.getDLERP().listProductByProfits(form/100, to/100));
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}

	protected void listByShard() {
		
		try {
			tableModel.resert(mode.getDLERP().listProductByShared());
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
		
	}

	protected void listByBom() {
		try {
			tableModel.resert(mode.getDLERP().listProductByBOM());
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
		
	}

	protected void listByPriceError() {
		try {
			tableModel.resert(mode.getDLERP().listProductByErrorPrice());
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
		
	}

	protected void m_jResert1ActionPerformed(ActionEvent e) {
		beforeFilterParas=null;
		timeCycleType =QuickTimerFilter.TIMERCYCLETYPE_Null;
		jTxtStartDate.setText(null);
		jTxtEndDate.setText(null);
	}

	protected void m_jBarcode1ActionPerformed(ActionEvent e) {
		String sCode = inputCode.getText();
		if (sCode.isEmpty()) {
			return;
		}
		if (sCode.matches("^[~～][0-9]+")) {
			List<Product> ret=new ArrayList<Product>();
			try {
				Product proc=ProductSwingUtil.getProductInfo(this, mode.getDLERP(),sCode.substring(1));
				if(proc!=null){
					ret.add(proc);
					ret.addAll(mode.getDLERP().getBomProductForIncludeListById(proc.getID()));
					ret.addAll(mode.getDLERP().getBomIncludeProductListById(proc.getID()));
					if(proc.getM_sSimilarCode()!=null&&proc.getM_sSimilarCode().length()>1)
						ret.addAll(mode.getDLERP().getProductBySimilarCodeNoSrc(proc.getID(),proc.getM_sSimilarCode()));
				}
			} catch (BasicException e1) {
				new MessageInf(e1).show(this);
			}
			tableModel.resert(ret);
			return;
			
		} else {
			try {
				tableModel.resert(mode.getDLERP().query(Product.class,  ProductUtil.getProductSeach(sCode.trim())));
			} catch (BasicException e1) {
				new MessageInf(e1).show(this);
				e1.printStackTrace();
			}
		}
		
	}

	public void reQuery() {
		try {
			tableModel.resert(mode.getDLERP().getProductList(beforeFilterParas));
			if (beforeFilterParas[11] != null)
				jTxtStartDate.setText(Formats.TIMESTAMP
						.formatValue(beforeFilterParas[11]));
			else
				jTxtStartDate.setText(null);
			if (beforeFilterParas[13] != null)
				jTxtEndDate.setText(Formats.TIMESTAMP
						.formatValue(beforeFilterParas[13]));
			else
				jTxtEndDate.setText(null);
		} catch (BasicException ex) {
			new MessageInf(ex).show(this);
		}
	}
	
	@Override
	public Object createValue() throws BasicException {
		Object[] afilter = new Object[14];

		afilter[0] = QBFCompareEnum.COMP_NONE;
		afilter[1] = null;

		// afilter[0] = QBFCompareEnum.COMP_RE;
		// afilter[1] = "%" + m_jtxtName.getText() + "%";

		// Precio de compra
		afilter[2] = QBFCompareEnum.COMP_NONE;
		afilter[3] = null;

		// Precio de venta
		afilter[5] = null;
		afilter[4] = QBFCompareEnum.COMP_NONE;

		// Categoria
		afilter[6] = QBFCompareEnum.COMP_NONE;
		afilter[7] = null;

		// el codigo de barras
		afilter[8] = QBFCompareEnum.COMP_NONE;
		afilter[9] = null;

		// afilter[8] = QBFCompareEnum.COMP_RE;
		// afilter[9] = "%" + m_jtxtBarCode.getText() + "%";

		// Date range
		Object startdate = Formats.TIMESTAMP
				.parseValue(jTxtStartDate.getText());
		Object enddate = Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());

		afilter[10] = (startdate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_GREATEROREQUALS;
		afilter[11] = startdate;
		afilter[12] = (enddate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_LESS;
		afilter[13] = enddate;

		return afilter;
	}

	@Override
	public String getTitle() {
		return AppLocal.getIntString("Menu.ProdcutFinishing");
	}

	@Override
	public void activate() throws BasicException {
        productCategoryNavigator.loadData();
        operationProductCategoryNavigator.loadData();
        customerNavigator.loadData();
	}

	@Override
	public boolean deactivate() {
		return true;
	}

	@Override
	public JComponent getComponent() {
		return this;
	}

	@Override
	public Object getBean() {
		return this;
	}

	@Override
	public void initFinishPanel(AppView app) throws BeanFactoryException {
		timeCycleType = QuickTimerFilter.TIMERCYCLETYPE_Null;
		productCategoryNavigator = new JProductCategoryNavigator(mode.getDLERP());
		productCategoryNavigator.addProductCategoryChangedListener(new ProductCategoryChangedListener(){
			@Override
			public void valueSelectChanged(ProductCategory obj) {
				if (beforeFilterParas == null)
					try {
						beforeFilterParas = (Object[]) createValue();
					} catch (BasicException e) {
						new MessageInf(e).show(JProductsFinishingPanel.this);
						return;
					}
					// Categoria
					beforeFilterParas[6] = QBFCompareEnum.COMP_EQUALS;
					beforeFilterParas[7] = obj.getID();
					reQuery();
				
			}
			
		});
		jNavigator.add(productCategoryNavigator, "category");

		operationProductCategoryNavigator = new JOperationProductCategoryNavigator(
				mode.getDLERP());
		operationProductCategoryNavigator
				.addOperationProductCategoryChangedListener(new OperationProductCategoryChangedListener(){

					@Override
					public void valueSelectChanged(OperationProductCategory obj) {
						try {
							tableModel.resert(mode.getDLERP().listByOperationCategory(obj.getID()));
						} catch (BasicException e) {
							new MessageInf(e).show(JProductsFinishingPanel.this);
						}
						
					}
					
				});
		jNavigator.add(operationProductCategoryNavigator, "operationCategory");
		
		customerNavigator=new JCustomerNavigator(mode.getDLERP());
        customerNavigator.addCustomerChangedListener(new CustomerChangedListener(){

			@Override
			public void valueSelectChanged(CustomerInfo obj) {
				if(beforeFilterParas==null)
					try {
						beforeFilterParas=(Object[]) createValue();
					} catch (BasicException e) {
						new MessageInf(e).show(JProductsFinishingPanel.this);
						return;
					}
					try {
						tableModel.resert(mode.getDLERP().listProductByCustomer(obj.getId()));
					} catch (BasicException e) {
						new MessageInf(e).show(JProductsFinishingPanel.this);
					} 	
			}
        	
        });
        jNavigator.add(customerNavigator, "customer");
		
		bomProductPanel=new  JChildDAOEditorPanel(mode.getDLERP(),new BomProductTableModel(),new ProductBomEditor(mode.getDLERP(),new DirtyManager()));
		linkTabPanel.add(bomProductPanel.getTabName(), bomProductPanel);
        addParentNodeChangeListener(bomProductPanel);

        productCustomerPriceEditor=new  JChildDAOEditorPanel(mode.getDLERP(),new ProductCustomerCategoryPriceModel(),new ProductCustomerPriceEditor(mode.getDLERP(),new DirtyManager()));
        linkTabPanel.add(productCustomerPriceEditor.getTabName(), productCustomerPriceEditor);
        addParentNodeChangeListener(productCustomerPriceEditor);
        
        productCodeEditor=new  JChildDAOEditorPanel(mode.getDLERP(),new ProductCodeTableModel(),new ProductCodeEditor(new DirtyManager()));
        linkTabPanel.add(productCodeEditor.getTabName(), productCodeEditor);
        addParentNodeChangeListener(productCodeEditor);
        
        productPriceEditor=new JChildDAOEditorPanel(mode.getDLERP(),new ProductPriceTableModel(),new ProductPriceEditor(new DirtyManager()));
        linkTabPanel.add(productPriceEditor.getTabName(), productPriceEditor);
        addParentNodeChangeListener(productPriceEditor);
        
        productScoreEditor=new JChildDAOEditorPanel(mode.getDLERP(),new ProductScoreTableModel(),new ProductScoreEditor(new DirtyManager()));
        linkTabPanel.add(productScoreEditor.getTabName(), productScoreEditor);
        addParentNodeChangeListener(productScoreEditor); 
        
        productAuxiliarEditor=new  JChildDAOEditorPanel(mode.getDLERP(),new ProductAuxiliarTableModel(),new AuxiliarEditor(mode.getDLERP(),new DirtyManager()));
        linkTabPanel.add(productAuxiliarEditor.getTabName(), productAuxiliarEditor);
        addParentNodeChangeListener(productAuxiliarEditor);
        
        productStockLevelEditor=new JChildDAOEditorPanel(mode.getDLERP(),new ProductStockLevelTableModel(),new ProductStockLevelEditor(mode.getDLERP(),new DirtyManager()));
        linkTabPanel.add(productStockLevelEditor.getTabName(), productStockLevelEditor);
        addParentNodeChangeListener(productStockLevelEditor);
        
       
        
        linkTabPanel.add(AppLocal.getIntString("Menu.Categories"),new JDropProductByCategoryPanel(mode.getDLERP()));
        linkTabPanel.add(AppLocal.getIntString("label.operationProductCategory"),new JDropProductByOperationCategoryPanel(mode.getDLERP()));
        linkTabPanel.add("Alt+"+AppLocal.getIntString("label.ProductDrop"),new JDropProductByProductPanel(mode.getDLERP()));
        linkTabPanel.add(JDropProductByCustomerPanel.getTabName(),new JDropProductByCustomerPanel(mode.getDLERP()));

        
        
        JProductCodeSimilarPanel jProductCodeSimilarPanel=new JProductCodeSimilarPanel(mode.getDLERP());
        linkTabPanel.addTab(AppLocal.getIntString("label.SimilarCodeProduct"),jProductCodeSimilarPanel);
        linkTabPanel.addTab(AppLocal.getIntString("label.operationProductCategory"), new JOperationProductCategoryPanel(mode.getDLERP()));
        JProductStorePanel productStorePanel=new JProductStorePanel(mode.getDLERP());
        linkTabPanel.addTab(AppLocal.getIntString("label.prodstock"), productStorePanel);
        linkTabPanel.addTab(AppLocal.getIntString("label.SimilarProductStore"), new JSimilarProductStorePanel(mode.getDLERP()));
        
        linkTabPanel.addChangeListener(new ChangeListener(){
			@Override
            public void stateChanged(ChangeEvent e) {
				fireBaseBeanChanged(keyed,null);
            }
        });
        JProductBomIncludeStorePanel productBomIncludeStorePanel=new JProductBomIncludeStorePanel(mode.getDLERP());
        linkTabPanel.addTab(AppLocal.getIntString("label.prodBomIncludeStock"), productBomIncludeStorePanel);
        
        JProductIncludeBomStorePanel productIncludeBomStorePanel=new JProductIncludeBomStorePanel(mode.getDLERP());
        linkTabPanel.addTab(AppLocal.getIntString("label.prodIncludeBomStock"), productIncludeBomStorePanel);
        
        JProductLotNoStorePanel productLotNoStorePanel=new JProductLotNoStorePanel(mode.getDLERP());
        linkTabPanel.addTab(AppLocal.getIntString("label.lotNoStore"), productLotNoStorePanel);
        
        JBomProductForIncludePanel jBomProductForIncludePanel=new JBomProductForIncludePanel(mode.getDLERP());
        linkTabPanel.addTab(AppLocal.getIntString("label.prodIncludeBom"),jBomProductForIncludePanel);
        
        JProductCustomerViewPanel jProductCustomerViewPanel=new JProductCustomerViewPanel(mode.getDLERP());
        linkTabPanel.addTab(AppLocal.getIntString("label.productCustomerView"),jProductCustomerViewPanel);
	   
        linkTabPanel.add(JDropProductByConcernsPanel.getTabName(),new JDropProductByConcernsPanel(mode.getDLERP()));
	}

	JTextField ProfitsFrom;
	JTextField profitsTo;
	
    protected void configNavigator(JPanel navigatorToolBar,JPanel navigator){
		JButton button_2 = new JButton(AppLocal.getIntString("label.prodcategory"));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl1.show(jNavigator, "category");
			}
		});
		navigatorToolBar.add(button_2);

		JButton btnNewButton = new JButton(
				AppLocal.getIntString("label.operationProductCategory"));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl1.show(jNavigator, "operationCategory");
			}
		});
		navigatorToolBar.add(btnNewButton);
		
		JButton button_11 = new JButton(AppLocal.getIntString("label.customer"));
        button_11.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		cl1.show(jNavigator, "customer");
        	}
        });
        navigatorToolBar.add(button_11);
		
        JButton btnNewButton1 = new JButton(
				AppLocal.getIntString("label.prodConcerns"));
		btnNewButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl1.show(jNavigator, "prodConcerns");
			}
		});
		navigatorToolBar.add(btnNewButton1);
        
		JPanel panel_3 = new JPanel();
		jNavigator.add(panel_3, "QBFPanel");
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_2.setPreferredSize(new Dimension(10, 155));
		panel_3.add(panel_2, BorderLayout.NORTH);
		GridBagLayout gbl_panel_2 = new GridBagLayout();
		gbl_panel_2.columnWidths = new int[]{120, 120, 0};
		gbl_panel_2.rowHeights = new int[]{31, 31, 31, 0};
		gbl_panel_2.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_panel_2.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel_2.setLayout(gbl_panel_2);
		
		JButton button_4 = new JButton(AppLocal.getIntString("Menu.SharedProducts"));
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listByShard();
			}
		});
		
		JButton button_1 = new JButton(AppLocal.getIntString("label.productPriceError"));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listByPriceError();
			}
		});
		GridBagConstraints gbc_button_1 = new GridBagConstraints();
		gbc_button_1.fill = GridBagConstraints.BOTH;
		gbc_button_1.insets = new Insets(0, 0, 5, 5);
		gbc_button_1.gridx = 0;
		gbc_button_1.gridy = 0;
		panel_2.add(button_1, gbc_button_1);
		
		JButton button_8 = new JButton(AppLocal.getIntString("label.BOM"));
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listByBom();
			}
		});
		GridBagConstraints gbc_button_8 = new GridBagConstraints();
		gbc_button_8.fill = GridBagConstraints.BOTH;
		gbc_button_8.insets = new Insets(0, 0, 5, 0);
		gbc_button_8.gridx = 1;
		gbc_button_8.gridy = 0;
		panel_2.add(button_8, gbc_button_8);
		
		JButton button_9 = new JButton(AppLocal.getIntString("Menu.PrefPrice"));
		GridBagConstraints gbc_button_9 = new GridBagConstraints();
		gbc_button_9.fill = GridBagConstraints.BOTH;
		gbc_button_9.insets = new Insets(0, 0, 5, 5);
		gbc_button_9.gridx = 0;
		gbc_button_9.gridy = 1;
	    panel_2.add(button_9, gbc_button_9);
		GridBagConstraints gbc_button_4 = new GridBagConstraints();
		gbc_button_4.fill = GridBagConstraints.BOTH;
		gbc_button_4.insets = new Insets(0, 0, 5, 0);
		gbc_button_4.gridx = 1;
		gbc_button_4.gridy = 1;
		panel_2.add(button_4, gbc_button_4);
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_4.getLayout();
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.gridwidth = 2;
		gbc_panel_4.fill = GridBagConstraints.BOTH;
		gbc_panel_4.gridx = 0;
		gbc_panel_4.gridy = 2;
		panel_2.add(panel_4, gbc_panel_4);
		
		JLabel lblNewLabel = new JLabel(AppLocal.getIntString("label.profits"));
		panel_4.add(lblNewLabel);
		
		ProfitsFrom = new JTextField();
		panel_4.add(ProfitsFrom);
		ProfitsFrom.setColumns(8);
		
		JLabel label_1 = new JLabel("-");
		panel_4.add(label_1);
		
		profitsTo = new JTextField();
		panel_4.add(profitsTo);
		profitsTo.setColumns(8);
		
		JButton btnNewButton_5 = new JButton("");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listByProfits();
			}
		});
		btnNewButton_5.setIcon(new ImageIcon(JProductsFinishingPanel.class.getResource("/com/openbravo/images/apply.png")));
		panel_4.add(btnNewButton_5);
		
		JQBFParametersPanel qbfPanel=new JQBFParametersPanel(){

			@Override
			public void doQBFParametersFilter(QBFParameters paras) {
				try {
					tableModel.resert(mode.getDLERP().query(Product.class, paras));
				} catch (BasicException e) {
					new MessageInf(e).show(this);
				}
			}
        };
        qbfPanel.setShowFieldCount(6);
        qbfPanel.setFields(ProductTableModel.getQBFFields());
        panel_3.add(qbfPanel, BorderLayout.CENTER);
    }
  
	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if (beforeFilterParas == null)
			try {
				beforeFilterParas = (Object[]) createValue();
			} catch (BasicException ex) {
				new MessageInf(ex).show(this);
				return;
		}
		
		// Date range
		Object startdate = e.getStartDate();
		Object enddate = e.getEndDate();

		beforeFilterParas[10] = (startdate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_GREATEROREQUALS;
		beforeFilterParas[11] = startdate;
		beforeFilterParas[12] = (enddate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_LESS;
		beforeFilterParas[13] = enddate;
		reQuery();
		
	}


	@Override
	protected AbstractDAOJEditor<ProductExt> getEditor() {
		return new ProductsEditor(mode.getDLERP(), new DirtyManager());
	}

	@Override
	protected Product convectToViewModel(ProductExt obj) {
		return obj;
	}

	@Override
	protected ProductExt convectToEditModel(Product obj) throws BasicException{
		try {
			return (ProductExt) mode.getDLERP().findByStringId(ProductExt.class,obj.getID());
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		};
		return null;
	}

	@Override
	public String getModelName() {	
		return ERP_UI_Swing_Model.MODEL_NAME;
	}

}
